﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CNotebook.Inventory
{
    public class ProductManager:ConnectionManager
    {
        #region Add/Update
        public void AddProduct(string _BrandID, string _Category, string _ProductCode, string _ProductName)
        {
            SqlCommand oCommand = new SqlCommand();
            oCommand.Connection = this.Connection;
            oCommand.CommandText = "INSERT INTO M_BRND_CT_PRD (BrandID, CatgryCode, ProdCode, ProductName) VALUES (@BrandID, @CatgryCode,@ProdCode, @ProductName)";
            oCommand.Parameters.Add(new SqlParameter("@BrandID", _BrandID));
            oCommand.Parameters.Add(new SqlParameter("@CatgryCode", _Category));
            oCommand.Parameters.Add(new SqlParameter("@ProdCode", _ProductCode));
            oCommand.Parameters.Add(new SqlParameter("@ProductName", _ProductName));

            oCommand.ExecuteNonQuery();
        }

        public void UpdateProduct(string _BrandID, string _CategoryCode, string _ProductCode, string _ProductName)
        {
            SqlCommand oCommand = new SqlCommand();
            oCommand.Connection = this.Connection;
            oCommand.CommandText = "UPDATE M_BRND_CT_PRD SET ProductName=@ProductName WHERE ProdCode=@ProdCode AND BrandID=@BrandID AND CatgryCode=@CatgryCode";
            oCommand.Parameters.Add(new SqlParameter("@BrandID", _BrandID));
            oCommand.Parameters.Add(new SqlParameter("@CatgryCode", _CategoryCode));
            oCommand.Parameters.Add(new SqlParameter("@ProdCode", _ProductCode));
            oCommand.Parameters.Add(new SqlParameter("@ProductName", _ProductName));

            oCommand.ExecuteNonQuery();
        }

        #endregion

        public DataTable GetProductsByBrandID(string _BrandID, string _Category)
        {
            DataTable _table = new DataTable();

            DataSet ds = new DataSet();
            SqlDataAdapter oAdapter = new SqlDataAdapter();

            SqlCommand oCommand = new SqlCommand();
            oCommand.Connection = this.Connection;
            oCommand.CommandText = "SELECT * FROM M_BRND_CT_PRD WHERE BrandID=@BrandID AND CatgryCode=@CatgryCode";
            oCommand.Parameters.Add(new SqlParameter("@BrandID", _BrandID));
            oCommand.Parameters.Add(new SqlParameter("@CatgryCode", _Category));

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(ds);

            _table = ds.Tables[0];

            return _table;
        }

        public DataTable GetProducts(string _BrandID, string _Category)
        {
            DataTable _table = new DataTable();

            DataSet ds = new DataSet();
            SqlDataAdapter oAdapter = new SqlDataAdapter();

            SqlCommand oCommand = new SqlCommand();
            oCommand.Connection = this.Connection;
            oCommand.CommandText = "SELECT * FROM M_BRND_CT_PRD WHERE BrandID=@BrandID AND CatgryCode=@CatgryCode";
            oCommand.Parameters.Add(new SqlParameter("@BrandID", _BrandID));
            oCommand.Parameters.Add(new SqlParameter("@CatgryCode", _Category));

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(ds);

            _table = ds.Tables[0];

            return _table;
        }

        public bool IsExists(string _ProdCode)
        {
            DataSet ds = new DataSet();
            SqlDataAdapter oAdapter = new SqlDataAdapter();
            SqlCommand oCommand = new SqlCommand();
            oCommand.Connection = this.Connection;
            oCommand.CommandText = "SELECT * FROM M_BRND_CT_PRD WHERE ProdCode=@ProdCode";
            oCommand.Parameters.Add(new SqlParameter("@ProdCode", _ProdCode));
            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(ds);

            DataTable _table = ds.Tables[0];

            int iCount = _table.Rows.Count;

            if (iCount > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
